package com.gsn.chooseChannel;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.GsnImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.gsn.dataCommon.ConfigModel;
import com.gsn.dataCommon.UserData;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.gdx.GsnParticleActor;
import com.gsn.engine.myplay.GsnLayer;
import com.gsn.poker.asset.FontTexture;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class GiftInitLayer extends GsnLayer implements ClickListener {
	private static final String tag = "DialogLayer";
	float time = 0.5f;
	protected ParticleEffect clickEff;
	public int id;

	public boolean init = false;

	public GiftInitLayer(float width, float height) {
		super(width, height);

		// scrollShopGold.y = bgQuickShop.y + bgQuickShop.height * 0.02f;
		// scrollShopGold.x = bgQuickShop.x + (bgQuickShop.width -
		// scrollShopGold.width)/2;
	}

	public void init() {
		if (init)
			return;
		PortalTexture portalTexture = PortalTexture.inst();
		FontTexture fontTexture = FontTexture.inst();

		Image bgDark = new Image(PortalTexture.inst().bgDarkDialog);
		PortalTexture.inst().scaleActor(bgDark);
		addActor(bgDark);
		bgDark.width = width;
		bgDark.height = height;

		Image bg = new Image(PortalTexture.inst().bgGift);
		PortalTexture.inst().scaleActor(bg);
		addActor(bg);
		bg.x = (width - bg.width) / 2;
		bg.y = (height - bg.height) / 2;

		float pad = width * 0.01f;

		Image gift1;
		Image gift2;
		Image gift3;
		Image gift4;
		Image gift5;

		Image gold1;
		Image gold2;
		Image gold3;
		Image gold4;
		Image gold5;

		Image deco;
		deco = new Image(PortalTexture.inst().highLight);
		portalTexture.scaleActor(deco);
		int index = UserData.giftIndex;
		if (index == 1) {
			gift1 = new Image(PortalTexture.inst().gift1);
			gold1 = new Image(PortalTexture.inst().goldGift);

		} else {
			gift1 = new Image(PortalTexture.inst().gift1Disable);
			gold1 = new Image(PortalTexture.inst().goldGiftDisable);
		}

		if (index == 5) {
			gift5 = new Image(PortalTexture.inst().gift5);

			gold5 = new Image(PortalTexture.inst().goldGift);
		} else {
			gift5 = new Image(PortalTexture.inst().gift5Disable);
			gold5 = new Image(PortalTexture.inst().goldGiftDisable);

		}

		if (index == 2) {
			gold2 = new Image(PortalTexture.inst().goldGift);
			gift2 = new Image(PortalTexture.inst().gift234);

		} else {
			gift2 = new Image(PortalTexture.inst().gift234Disable);
			gold2 = new Image(PortalTexture.inst().goldGiftDisable);
		}

		if (index == 3) {
			gold3 = new Image(PortalTexture.inst().goldGift);
			gift3 = new Image(PortalTexture.inst().gift234);

		} else {
			gift3 = new Image(PortalTexture.inst().gift234Disable);
			gold3 = new Image(PortalTexture.inst().goldGiftDisable);
		}

		if (index == 4) {
			gold4 = new Image(PortalTexture.inst().goldGift);
			gift4 = new Image(PortalTexture.inst().gift234);

		} else {
			gift4 = new Image(PortalTexture.inst().gift234Disable);
			gold4 = new Image(PortalTexture.inst().goldGiftDisable);
		}

		PortalTexture.inst().scaleActor(gift1);
		PortalTexture.inst().scaleActor(gift2);
		PortalTexture.inst().scaleActor(gift3);
		PortalTexture.inst().scaleActor(gift4);
		PortalTexture.inst().scaleActor(gift5);

		PortalTexture.inst().scaleActor(gold1);
		PortalTexture.inst().scaleActor(gold2);
		PortalTexture.inst().scaleActor(gold3);
		PortalTexture.inst().scaleActor(gold4);
		PortalTexture.inst().scaleActor(gold5);

		gift1.x = bg.x + 0.05f * bg.width;
		gift2.x = bg.x + 0.22f * bg.width;
		gift3.x = bg.x + 0.4f * bg.width;
		gift4.x = bg.x + 0.58f * bg.width;
		gift5.x = bg.x + 0.76f * bg.width;

		gift1.y = bg.y + (bg.height * 1.2f - gift1.height) / 2;
		gift2.y = gift1.y;
		gift3.y = gift1.y;
		gift4.y = gift1.y;
		gift5.y = gift1.y;

		Label labelGold1;
		Label labelGold2;
		Label labelGold3;
		Label labelGold4;
		Label labelGold5;
		if (index == 1) {
			gift1.x = gift1.x - 0.012f * bg.width;
			deco.x = gift1.x + (gift1.width - deco.width) / 2;
			deco.y = gift1.y + (gift1.height - deco.height) / 2;

		} else if (index == 2) {
			gift2.x = gift2.x - 0.01f * bg.width;
			deco.x = gift2.x + (gift2.width - deco.width) / 2;
			deco.y = gift2.y + (gift2.height - deco.height) / 2;
		} else if (index == 3) {
			gift3.x = gift3.x - 0.01f * bg.width;
			deco.x = gift3.x + (gift3.width - deco.width) / 2;
			deco.y = gift3.y + (gift3.height - deco.height) / 2;
		} else if (index == 4) {
			gift4.x = gift4.x - 0.01f * bg.width;
			deco.x = gift4.x + (gift4.width - deco.width) / 2;
			deco.y = gift4.y + (gift4.height - deco.height) / 2;
		} else if (index == 5) {
			// gift5.x = gift5.x - 0.012f * bg.width;
			deco.x = gift5.x + (gift5.width - deco.width) / 2;
			deco.y = gift5.y + (gift5.height - deco.height) / 2;

		}

		gold1.x = gift1.x + (gift1.width * 0.8f - gold1.width) / 2;
		gold2.x = gift2.x + (gift2.width * 1f - gold2.width) / 2;
		gold3.x = gift3.x + (gift3.width * 1f - gold3.width) / 2;
		gold4.x = gift4.x + (gift4.width * 1f - gold4.width) / 2;
		gold5.x = gift5.x + (gift5.width * 1f - gold5.width) / 2;

		gold1.y = gift1.y + (gift1.height - gold1.height) / 2;
		gold2.y = gift1.y + (gift2.height - gold2.height) / 2;
		gold3.y = gift1.y + (gift3.height - gold3.height) / 2;
		gold4.y = gift1.y + (gift4.height - gold4.height) / 2;
		gold5.y = gift1.y + (gift5.height - gold5.height) / 2;

		addActor(gift1);
		addActor(gift2);
		addActor(gift3);
		addActor(gift4);
		addActor(gift5);

		addActor(deco);

		addActor(gold1);
		addActor(gold2);
		addActor(gold3);
		addActor(gold4);
		addActor(gold5);

		if (index == 1) {
			labelGold1 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[0].bean), new Label.LabelStyle(fontTexture.fontGift, new Color(1, 1, 1, 1)));
		} else {
			labelGold1 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[0].bean), new Label.LabelStyle(fontTexture.fontGiftDis, new Color(1, 1, 1, 1)));
		}

		if (index == 2) {
			labelGold2 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[1].bean), new Label.LabelStyle(fontTexture.fontGift, new Color(1, 1, 1, 1)));
		} else {
			labelGold2 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[1].bean), new Label.LabelStyle(fontTexture.fontGiftDis, new Color(1, 1, 1, 1)));
		}
		if (index == 3) {
			labelGold3 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[2].bean), new Label.LabelStyle(fontTexture.fontGift, new Color(1, 1, 1, 1)));
		} else {
			labelGold3 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[2].bean), new Label.LabelStyle(fontTexture.fontGiftDis, new Color(1, 1, 1, 1)));
		}
		if (index == 4) {
			labelGold4 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[3].bean), new Label.LabelStyle(fontTexture.fontGift, new Color(1, 1, 1, 1)));
		} else {
			labelGold4 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[3].bean), new Label.LabelStyle(fontTexture.fontGiftDis, new Color(1, 1, 1, 1)));
		}
		if (index == 5) {
			labelGold5 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[4].bean), new Label.LabelStyle(fontTexture.fontGift, new Color(1, 1, 1, 1)));
		} else {
			labelGold5 = new Label(ActorUtility.formatNumber(ConfigModel.giftModelList[4].bean), new Label.LabelStyle(fontTexture.fontGiftDis, new Color(1, 1, 1, 1)));
		}
		labelGold1.x = gift1.x + (gift1.width * 0.95f - labelGold1.getTextBounds().width) / 2;
		labelGold2.x = gift2.x + (gift2.width - labelGold2.getTextBounds().width) / 2;
		labelGold3.x = gift3.x + (gift3.width - labelGold3.getTextBounds().width) / 2;
		labelGold4.x = gift4.x + (gift4.width - labelGold4.getTextBounds().width) / 2;
		labelGold5.x = gift5.x + (gift5.width - labelGold5.getTextBounds().width) / 2;

		labelGold1.y = gift1.y + gift1.height * 0.1f;
		labelGold2.y = labelGold1.y;
		labelGold3.y = labelGold1.y;
		labelGold4.y = labelGold1.y;
		labelGold5.y = labelGold1.y;

		addActor(labelGold1);
		addActor(labelGold2);
		addActor(labelGold3);
		addActor(labelGold4);
		addActor(labelGold5);

		GsnImageButton receive = new GsnImageButton(portalTexture.iconReceiveGift, portalTexture.iconReceiveGiftPress);
		portalTexture.scaleActor(receive);
		addActor(receive);
		// receive.setClickListener(this);
		receive.x = bg.x + (bg.width - receive.getRealWidth()) / 2;
		receive.y = bg.y + bg.height * 0.1f;
		receive.setClickListener(new ClickListener() {

			@Override
			public void click(Actor actor, float x, float y) {
				// TODO Auto-generated method stub
				PokerSound.playClick();
				MyPoker.client.send(PacketFactory.getDailyGift());
				hide();
				UserData.giftIndex = 0;
			}
		});
	}

	public InputProcessor oldInput;

	public void show() {
		init();
		oldInput = Gdx.input.getInputProcessor();

		visible = true;
		setInputListener();
	}

	public void hide() {
		Gdx.input.setInputProcessor(oldInput);
		oldInput = null;
		visible = false;
	}

	public void showParticle(Actor actor, float x, float y) {
		GsnParticleActor par = new GsnParticleActor(PortalTexture.inst().clickEff);
		Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);

		par.setPostition(v.x, v.y);

		par.act(100000);
		addActor(par);
		par.start();
	}

	@Override
	public void click(Actor actor, float x, float y) {
		// TODO Auto-generated method stub
		System.out.println("click ");
	}

}
